package com.kaigao.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaigao.demo.constant.Constant;
import com.kaigao.demo.dao.mapper.ext.SanGuoUserDoMapperExt;
import com.kaigao.demo.dao.model.SanGuoUserDo;
import com.kaigao.demo.dto.SanGuoUserDto;
import com.kaigao.demo.utils.CommonValidate;
import com.kaigao.demo.utils.StringUtil;
import com.kaigao.demo.utils.ValidateException;
import com.kaigao.demo.vo.SanGuoUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNaME SanGuoUserService.java
 * @Description 功能描述：
 * @author 吴昊 2018年3月18日22:37:31
 */
@Service
public class SanGuoUserService {

    @Autowired
    private SanGuoUserDoMapperExt sanGuoUserMapperExt;

    /**
     * @Description 方法描述： 新增
     * @author 吴昊 2018年3月18日22:37:36
     * @return
     * @throws ValidateException
     * @throws Exception
     */
    public boolean add(SanGuoUserDo record) throws ValidateException {
        // 校验参数
        // ......
        return sanGuoUserMapperExt.insertSelective(record) == 1;
    }

    /**
     * @Description 方法描述： 修改
     * @author 吴昊 2018年3月18日22:37:45
     * @param record
     * @return
     * @throws ValidateException
     * @throws Exception
     */
    public boolean update(SanGuoUserDo record) throws ValidateException {
        // 校验参数
        // ......
        CommonValidate.ME.checkParam(record.getId());
        return sanGuoUserMapperExt.updateByPrimaryKeySelective(record) == 1;
    }

    /**
     * @Description 方法描述： 根据id查询详情
     * @author 吴昊 2018年3月18日22:37:53
     * @param id
     * @return
     * @throws ValidateException
     * @throws Exception
     */
    public SanGuoUserDo getById(Integer id) throws ValidateException {
        CommonValidate.ME.checkParam(id);
        return sanGuoUserMapperExt.selectByPrimaryKey(id);
    }

    /**
     * 方法描述:根据条件查询列表
     * 
     * @author 吴昊 2018年3月17日08:29:45
     * @return
     * @throws ValidateException
     * @throws Exception
     */
    public PageInfo<SanGuoUserVo> getList(SanGuoUserDto sanGuoUserDto) throws ValidateException {
        // 默认每页最大数量
        if (sanGuoUserDto != null) {
            if (sanGuoUserDto.getPageSize() > Constant.DEFAULT_MAX_PAGE_SIZE) {
                sanGuoUserDto.setPageSize(Constant.DEFAULT_MAX_PAGE_SIZE);
            }

            PageHelper.startPage(sanGuoUserDto.getPageNum(), sanGuoUserDto.getPageSize());
        }
        return new PageInfo<SanGuoUserVo>(sanGuoUserMapperExt.getList(sanGuoUserDto));

    }

    /**
     * 方法描述:批量逻辑删除
     * 
     * @author 吴昊 2018年3月17日08:30:00
     * @param ids 要删除的id组
     * @return
     * @throws ValidateException
     * @throws Exception
     */
    public boolean deletes(String ids) throws ValidateException {
        CommonValidate.ME.checkParam(ids);
        List<Long> idList = new ArrayList<>();
        for (String id : ids.split(",")) {
            idList.add(Long.valueOf(id));
        }
        if (idList.size() == 0) {
            return false;
        }
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("ids", idList);
        return sanGuoUserMapperExt.deletes(paramsMap) > 0;

    }

    /**
     * @Description 方法描述： 根据id查询列表
     * @author 吴昊 2018年3月17日08:30:15
     * @param ids
     * @return
     */
    public List<SanGuoUserVo> getListByIds(String ids) {
        List<Long> idList = new ArrayList<>();
        Map<String, Object> paramsMap = new HashMap<>();

        if (StringUtil.isNotBlank(ids)) {
            for (String id : ids.split(",")) {
                idList.add(Long.valueOf(id));
            }
            if (idList.size() == 0) {
                return null;
            }
            paramsMap.put("ids", idList);
        }
        return sanGuoUserMapperExt.getListByIds(paramsMap);
    }

}
